import getopt

from .autoService import multiRunner, log, AutoService


class test:
    pass


if __name__ == '__main__':
    import sys

    if len(sys.argv) < 2:
        print("usage: python -m core.multiRunner [moduleName].[[className].][flowName]")
        print("example:python -m core.multiRunner test.flow_login")
        print("example:python -m core.multiRunner test.TestA.flow_login")
        exit(0)
    filename=sys.argv[1]
    if filename.startswith(".\\"):filename=filename.replace(".\\","")
    if filename.startswith("./"): filename = filename.replace("./", "")
    args = filename.split(".")

    import importlib, inspect

    _cls = None  # type:
    _name = None
    flow_name = None
    if len(args) == 2:
        for name, cls in inspect.getmembers(importlib.import_module(args[0]), inspect.isclass):
            classes = cls.mro()
            if classes[0] == AutoService: continue
            if len(classes) < 3: continue
            if classes[-2] != AutoService: continue
            # index=str().find("AutoService")
            # if(index>0):
            _cls = cls
            _name = name
            flow_name = args[1]
            break
        if (_cls == None):
            print("自动装载类失败,类需要继承AutoService,否则需要手动传入")
            exit(0)
        # print(_cls)
        # print(_name)
        log("自动匹配到{}类".format(_name), model="multiRunner")
        # print()
    elif len(args) >= 3:
        try:
            mebers=inspect.getmembers(importlib.import_module(args[0]), inspect.isclass)
        except Exception as e:
            # print(e)
            print('注入失败，请把脚本放在autoService根目录')

            exit(0)
        for name, cls in mebers:
            classes = cls.mro()
            if classes[0] == AutoService: continue
            if len(classes) < 3: continue
            if classes[-2] != AutoService: continue
            if name != args[1]: continue

            # index=str().find("AutoService")
            # if(index>0):
            _cls = cls
            _name = name
            flow_name = args[2]
            break
        if (_cls == None):
            print("装载类失败,请手动检查类是否正确")
            exit(0)
    else:
        print("参数过多!")

    if (flow_name == "py"): flow_name = ""
    if (flow_name == ""):
        flows = list(filter(lambda name: name.startswith('flow_'), dir(_cls)))  # type:list
        if (len(flows) == 0):
            log("读取flow列表为空，需要在类里面定义!", model="multiRunner", level="ERROR")
            exit(0)
        elif (len(flows) == 1):
            log(f"正在装载{flows[1]}方法.")
            flow_name = flows[1]
        else:
            for name in flows:
                method = getattr(_cls, name)
                arrayData = getattr(method, '__annotations__')
                if 'default' in arrayData:
                    flow_name = name
                    break
            if (flow_name == ""):
                # command = 'python
                log("读取到多个flow名，请重新运行命令行或者在程序中指定默认flow入口", model="multiRunner", level="ERROR")
                # print(f"{command}\n")
                for name in flows:
                    print(name)
                exit(0)
            else:
                log(f"正在装载{flow_name}方法.")

    if hasattr(_cls, flow_name):
        multiRunner(_cls, flow_name)
    else:
        log(f"{flow_name},找不到该方法，无法继续执行")

    # multiRunner()
